<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ranking_model extends CI_Model {
	
	var $request_params;

	function __construct() {
		parent::__construct();

	}
	
	/**
	 * 스타 랭킹(월별 많이 본 스타) 리스트가져오기
	 */
	function select_star_ranking_list($service_id, $date) {
		log_message('debug',"#### Star_model::select_star_ranking_list($service_id, $date)");
		
		$year  = substr($date, 0, 4);
		$month = substr($date, 4, 2);
		
		$sql  = PHP_EOL." SELECT ";
		$sql .= PHP_EOL."       S.artist_idx ";
		$sql .= PHP_EOL."     , A.artist_name ";
		$sql .= PHP_EOL."     , A.job ";
		$sql .= PHP_EOL."     , S.year ";
		$sql .= PHP_EOL."     , S.month ";
		$sql .= PHP_EOL."     , SUM(S.cnt) cnt ";
		$sql .= PHP_EOL." FROM  tb_star_statistics S ";
		$sql .= PHP_EOL." INNER JOIN tb_artist A ON A.idx = S.artist_idx ";
		$sql .= PHP_EOL." WHERE S.year = ? ";
		$sql .= PHP_EOL." AND   S.month = ? ";
		$sql .= PHP_EOL." AND   S.service_id = ? ";
		$sql .= PHP_EOL." GROUP BY S.service_id, S.year, S.month, S.artist_idx ";
		$sql .= PHP_EOL." ORDER BY S.service_id, S.year DESC, S.month DESC, SUM(S.cnt) DESC ";
		$query = $this->db->query($sql, array($year, $month, $service_id)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 스타 궁합 조회수 수정
	 */
	function insert_update_star_statistics($service_id, $yyyy, $mm, $dd, $artist_idx, $cnt) {
		log_message('debug',"#### Star_model::insert_update_star_statistics($service_id, $yyyy, $mm, $dd, $artist_idx, $cnt)");
		
		$sql  = PHP_EOL." INSERT INTO tb_star_statistics ( ";
		$sql .= PHP_EOL."       service_id ";
		$sql .= PHP_EOL."     , year ";
		$sql .= PHP_EOL."     , month ";
		$sql .= PHP_EOL."     , day ";
		$sql .= PHP_EOL."     , artist_idx ";
		$sql .= PHP_EOL."     , cnt ";
		$sql .= PHP_EOL." ) VALUES ( ";
		$sql .= PHP_EOL."       '$service_id' ";
		$sql .= PHP_EOL."     , '$yyyy' ";
		$sql .= PHP_EOL."     , '$mm' ";
		$sql .= PHP_EOL."     , '$dd' ";
		$sql .= PHP_EOL."     , '$artist_idx' ";
		$sql .= PHP_EOL."     , '$cnt' ";
		$sql .= PHP_EOL." ) ON DUPLICATE KEY UPDATE ";
		$sql .= PHP_EOL."       service_id = '$service_id' ";
		$sql .= PHP_EOL."     , year = '$yyyy' ";
		$sql .= PHP_EOL."     , month = '$mm' ";
		$sql .= PHP_EOL."     , day = '$dd' ";
		$sql .= PHP_EOL."     , artist_idx = '$artist_idx' ";
		$sql .= PHP_EOL."     , cnt = cnt + $cnt";
		$sql .= PHP_EOL." ; ";
		
		$this->db->query($sql) or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}









	
	/**
	 * 인기스타 추가
	 */
	function insert_popular_star($popular_id, $artist_idx, $artist_name, $priority) {
		log_message('debug',"#### Popular_model::insert_popular_star($popular_id, $artist_idx, $artist_name, $priority)");

		$this->db->set('popular_id' , $popular_id);
		$this->db->set('artist_idx' , $artist_idx);
		$this->db->set('artist_name', $artist_name);
		$this->db->set('priority'   , $priority);
		$this->db->set('use_yn'    , 'Y');
		$this->db->insert('tb_popular_star') or $this->logsql->log_sql('error');

	}

	/**
	 * 인기스타 삭제
	 */
	function delete_popular_star($popular_id, $artist_idx) {
		log_message('debug',"#### Popular_model::delete_popular_star($popular_id, $artist_idx)");
		
		$this->db->where('popular_id',$popular_id);
		$this->db->where('artist_idx',$artist_idx);
		$this->db->delete('tb_popular_star') or $this->logsql->log_sql('error');
	}
	
}

/* End of file ranking_model.php */
/* Location: ./application/models/unse/ranking_model.php */